<?php
	/***
	 * 整体采集：用于整站循环
	 * 作者：elseksy
	 * 仅供测试使用，请勿用于商业用途 
	 */

	include_once('../sys/sys.php');
	include_once('./data/lndt.php');
	header('Content-Type: text/html; charset=utf-8');
	
	$action = $_GET['action'];
	if($action != 'acr'){
		show_normal_mesR('操作错误','操作错误','操作错误');
	}
	//获取全部小说列表
	$allcontents = file_get_contents($ln_list_url);
	$wjc = explode('<dl>',$allcontents);
	$wjc = explode('</dl>',$wjc[1]);
	$wjc = explode('<h3>',$wjc[0]);
	unset($wjc[0]);
	unset($allcontent);
	//每个数组为一个首字母分类
	
	//print_r($wjc);
	
	$wd = ord("A") - 2;
	$cwd = "";
	$ntotal = "";
	
	foreach ($wjc as $k=>$v) {
		//处理其它分类
		if($k == 1){
			$cwd = 'oth';
			//获取总量
			$ninfos = n_infos($v);
			$ntotal = count($ninfos['nid']);
			//获取数据库总量并比较
			$where = 'AND intchar="' . $cwd .'"';
			$dbcount = get_count($where);
			$tid = array();
			//如果当前数量大于数据库，则需要添加新增小说
			if($ntotal > $dbcount && $dbcount != 0){
				//取数据库中的ID
				$DB->build_query(array(
						'select' 	=> 'id',
						'from'		=> 'lnnovels',
						'where'	=> "1 " . $where ,
				));
				$qid = $DB->exec_query();
				while ($row = $DB->fetch_row($qid)) {
					$tid[] = $row['id'];
				}
				//通过ID挨个比较
				foreach ($ninfos['nid'] as $k => $nv) {
					//如果不存在，插入
					if (!in_array($nv, $tid)){
						do_ninsert($ninfos['nid'][$k], $cwd, addslashes($ninfos['name'][$k]), 0, 0);
						echo "<font color='green'>小说==" . $ninfos['name'][$k] . "==已添加</font><BR>";
						ob_flush();
						continue;
					}
// 					echo "<font color='red'>小说==" . $ninfos['name'][$k] . "==已存在，跳过</font><BR>";
// 					ob_flush();
				}
			}
			else if($dbcount == 0){//第一次采集
				for ($i = 0;$i < $ntotal; $i++){
					//挨个输入数据库(更新时间及总本数需要进入小说列表取得)
					do_ninsert($ninfos['nid'][$i], $cwd, addslashes($ninfos['name'][$i]), 0, 0);
					echo "小说==" . $ninfos['name'][$i] . "==已添加<BR>";
					ob_flush();
				}
			}			
			continue;
		}
		//处理从A到Z的首字母
		$cwd = chr($wd + $k);
		$ninfos = n_infos($v);
		$ntotal = count($ninfos['nid']);
		//获取数据库总量并比较
		$where = 'AND intchar="' . $cwd .'"';
		$dbcount = get_count($where);
		$tid = array();
		//如果当前数量大于数据库，则需要添加新增小说
		if($ntotal > $dbcount && $dbcount != 0){
			//取数据库中的ID
			$DB->build_query(array(
					'select' 	=> 'id',
					'from'		=> 'lnnovels',
					'where'	=> "1 " . $where ,
			));
			$qid = $DB->exec_query();
			while ($row = $DB->fetch_row($qid)) {
				$tid[] = $row['id'];
			}
			//通过ID挨个比较
			foreach ($ninfos['nid'] as $k => $nv) {
				//如果不存在，插入
				if (!in_array($nv, $tid)){
					do_ninsert($ninfos['nid'][$k], $cwd, addslashes($ninfos['name'][$k]), 0, 0);
					echo "<font color='green'>小说==" . $ninfos['name'][$k] . "==已添加</font><BR>";
					ob_flush();
					continue;
				}
// 				echo "<font color='red'>小说==" . $ninfos['name'][$k] . "==已存在，跳过</font><BR>";
// 				ob_flush();
			}
		}
		else if($dbcount == 0){//第一次采集
			for ($i = 0;$i < $ntotal; $i++){
				//挨个输入数据库(更新时间及总本数需要进入小说列表取得)
				do_ninsert($ninfos['nid'][$i], $cwd, addslashes($ninfos['name'][$i]), 0, 0);
				echo "小说==" . $ninfos['name'][$i] . "==已添加<BR>";
				ob_flush();
			}
		}
		continue;
		echo "<br>";
	}
	
	
	echo "采集完毕";
	ob_end_flush();
	exit;
	
	
	//获取每部小说的基本连接信息并返回数组
	function n_infos($wdcontent){
		$urln = '';
		$match_str = "/<a href=\"http:\/\/lknovel.lightnovel.cn\/main\/vollist\/(.*).html\" target=\"_blank\">(.*)<\/a>/isU";
		preg_match_all($match_str,$wdcontent,$urln);
		$urln['nid'] = $urln[1];
		$urln['name'] = $urln[2];
		unset($urln[0]);unset($urln[1]);unset($urln[2]);
		return $urln;
	}
	//获取数据库数量
	
	function get_count($where){
		global $DB;
		$result = $DB->build_and_exec_query(
				array('select' => 'count(*) AS count','from'   => 'lnnovels','where'	=> "1 " . $where ,)
		);
		return $result['count'];
	}
	//插入小说分类
	function do_ninsert($id,$intchar,$name,$lastupdate,$count){
		global $DB;
		$DB->do_insert( 'lnnovels', array(
				'id'				=> $id,
				'intchar'			=> $intchar,
				'name'				=> $name,
				'lastupdate'		=> $lastupdate,
				'count'				=> $count,
		)		);
		if ($result === false) {
			show_normal_mesR('无法添加当前小说','添加出错','添加');
		}
	}
	
	
	
	
	
	
	
	
	//以下为函数定义
	
	
	
	function getwj($count){
		//开始循环采集每个页面
		for($i = 1;$i <= $count ;$i++){
			$url = page_url_md($i);
			$allcontent = mb_convert_encoding(file_get_contents($url),"UTF-8","GB2312");
			$wjc = explode('状态</div></td>',$allcontent,2);
			$wjc = explode('</table></td></tr>',$wjc[1],2);
			//直接获取连接
			
			$match_str = "/<a href=(.*) target=\"_blank\"><font color=\"(.*)\">[目录]/isU";
			preg_match_all($match_str,$wjc[0],$urln);
			//获取单个小说的URL
			$urln = $urln[1];
			
			//print_r($urln);exit;
			
			foreach($urln as $k=>$v){
				//分离ID
				$id = explode('/',$v,6);
				//开始单页采集
				//ob_flush();
				//flush();
				getsig($id[4]);
			}
		}
		unset($allcontent);
	}
	
	
	function getsig($tid){
		global $DB;
		
		if(!is_numeric($tid) || $tid < 0){
			return;
		}
		//从TID获取文章列表连接
		$indexpre = 'http://www.xiaoshuo520.com/Book/';
		$indexadd =  '/Index.aspx';
		$indexurl = $indexpre . $tid . $indexadd;
		
		//echo $indexurl;exit;
		
		$c = file_get_contents($indexurl);
		$c = explode('<img src="/skin/default/images/yd_ico.gif" width="22" height="22" align="absmiddle">'
		,$c);
		$c = explode('<strong><a href="',$c[1]);
		$c = explode('"><',$c[1]);
		$c = $c[0];
		$base_url = 'http://www.xiaoshuo520.com';
		$url =  $base_url . $c;
		$tid = explode('/',$url);
		$eid = $tid[count($tid) - 3];
		$tid = $tid[count($tid) - 2];
		//分离URL以及ID
		$id = $tid;
		//定义公用变量
		//获取URL内容
		$content = 	file_get_contents($url);
		$content = 	mb_convert_encoding(file_get_contents($url),"UTF-8","GB2312");
		
		//获取名称
		$name = explode('</title>',$content);
		$name = $name[0];
		$name = explode('<title>',$name);
		$name = $name[1];
		$name = explode('最新章节列表',$name);
		$name = $name[0];
		
		//获取最后更新时间
		$dt = explode('</b>于',$content);
		$dt = $dt[1];
		$dt = explode('由',$dt);
		$dt = strtotime(trim($dt[0]));
		
		//获取URL列表
		$urls = explode('<div id="BookText"><br />',$content);
		$urls = $urls[1];
		$urls = explode('<div id="End">',$urls);
		$urls = $urls[0];
		$replacer = array('<div id="ListEnd"></div>','<li>','</li>');
		$replacerc = array('','',"\r\n");
		$urls = str_replace($replacer,$replacerc,$urls);
		$match_str = "/<a href=\"(.*)\" title=\"(.*)\">/isU";
		preg_match_all($match_str,$urls,$urln);
		//获取每章连接以及章节标题和更新时间
		$urlns = $urln[1];
		$titlens = $urln[2];
		
		
		
		unset($urln);
		//获取每单位总数
		$ulnum = count($urlns);
		//进入每页进行处理
		//插入分类信息
		//检查当前ID是否存在
		$where = 'AND id= ' . $id .'';
		//if($id != ''){
            $result = $DB->build_and_exec_query(
			array('select' => 'COUNT(*) as max','from'   => '520tags','where'	=> "1 " . $where ,));
		//}
		//print_r($DB);exit;
		//else
		//{
        //    return;
		//}
		//当已经存在的时候更新日期
		if ($result['max'] > 0) {
			
		}
		//当不存在时插入
		else{
			do_taginsert($id,$eid,$dt,$name,0);
		}
		echo '<font color="blue">正在添加小说：'. $name . '</font><BR>';
		//获取数据库COUNT
		$where = 'AND id= ' . $id .'';
			$result = $DB->build_and_exec_query(
				array('select' => 'count','from'   => '520tags','where'	=> "1 " . $where ,)
			);	
		$dbcount = $result['count'];
		
		foreach ($urlns as $k=>$v) {
			//当COUNT小于已经采集的时候，跳过。
			if ($k < $dbcount) {
				continue;
			}
			//老版直接采集IF
			$comic_contents = mb_convert_encoding(file_get_contents($url.$v),"UTF-8","GB2312");
			//从IF中获取连接
			$dtd = explode('border:0;" src="',$comic_contents);
			$dtd = explode('" height="100%',$dtd[1]);
	//		echo $dtd[0];exit;
			
			//新版的方法
	//		$v = str_replace('.shtml','',$v);
	//		$current_url = $base_url . '/Html/ShowChapter.aspx?cid=' . $v . '&bid=' . $tid;
	//		$comic_contents = mb_convert_encoding(file_get_contents($current_url),"UTF-8","GB2312");
	//		//由于改版，需要进一步获取连接
	//		//获取最新位置
	//		$dtd = explode('<td>&nbsp;&nbsp;<a href="',$comic_contents);
	//		$dtd = explode('" target="_parent">',$dtd[1]);
			
			
			$comic_contents = mb_convert_encoding(file_get_contents($dtd[0]),"UTF-8","GB2312");
			if ($comic_contents == '') {
				//注意如果这里出错了，则该页将无法采集。
				continue;
			}
			//获取文章内容，这部分涉及到判断是否为图片，我的意见是直接将图片存成连接文字。
			//确定处理范围
			$comic_contents = explode('<div id="BookText"><br />&nbsp;&nbsp;&nbsp;&nbsp;',$comic_contents);
			$comic_contents = explode("<script type='text/javascript' src='http://drmcmm.baidu.com/js/s.js'></script>",$comic_contents[1]);
			//替换关键字定义
			$replacer = array(' ',"<div style='display:none'>",'&nbsp;','</div>',"<divstyle='display:none'>",'言情言情言情言情言情','文学文学文学文学文学','书库书库书库书库书库','网络文学网络文学网络文学网络文学网络文学','武侠武侠武侠武侠武侠','网游网游网游网游网游','玄幻玄幻玄幻玄幻玄幻','奇幻奇幻奇幻奇幻奇幻','杂志杂志杂志杂志杂志','小说小说小说小说小说','散文散文散文散文散文','诗歌诗歌诗歌诗歌诗歌','杂谈杂谈杂谈杂谈杂谈');
			$replacerc = array('');
			$text = str_replace($replacer,$replacerc,$comic_contents[0]);
			$replacer = array('<br>','<br/><br/>','<br/>');
			$replacerc = array('<br/>','<br/>',"\r\n");
			$text = str_replace($replacer,$replacerc,$text);
			$text = htmlspecialchars($text);
			//判断图像
			$ispic = 0;
			if (strstr($text,'img')) {
				$ispic = 1;
			}
			//获取章节名称
			$title = $titlens[$k];
			$title = explode('更新时间',$title);
			$title = explode('章节名:',$title[0]);
			
			//这里添加数据库插入语句
			do_photoinsert($tid,$dtd[0],$text,$title[1],0,$ispic);
			echo '<font color="green">已成功添加章节：'. $title[1] . '</font><BR>';
		}
		
		//更新计数
		$where = 'id= ' . $id .'';
		$DB->do_update('520tags',
			array(					  
			   'lastupdate'				=> $dt,
			   'count'					=> count($urlns),
			 ),
			 $where
		);
	}
	
	function do_taginsert($id,$eid,$lu,$name,$count){
		global $DB;
		$DB->do_insert( '520tags', array( 'id'			=> $id,
								   'eid'					=> $eid,
								   'lastupdate'				=> $lu,
								   'name'					=> $name,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前小说','添加出错','添加');
		}
	}
	
	
	function do_photoinsert($uid,$url,$text,$title,$count,$ispic){
		global $DB;
		$DB->do_insert( '520photos', array( 
								   'uid'				=> $uid,
								   'url'					=> $url,
								   'ispic'					=> $ispic,
								   'name'					=> $title,
								   'text'					=> $text,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前小说','添加出错','添加');
		}
	}
	
	function page_url_md($pg){
		$base_list_url = 'http://www.xiaoshuo520.com/Book/ShowBooklist.aspx?page=';
		return $base_list_url . $pg;
	}
	
	
?>